package com.bigevent.service.user.impl;

import com.bigevent.mapper.user.HostListMapper;
import com.bigevent.pojo.Article;
import com.bigevent.service.user.HostListService;
import com.bigevent.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class HostListServiceImpl implements HostListService {

    @Autowired
    private HostListMapper hostListMapper;

    @Override
    public List<Article> selectHostList() {
        List<Article> list = hostListMapper.selectHostList();
        return list;
    }

    @Override
    public Article selectHostDetail(Integer id) {
        Article article = hostListMapper.selectHostDetail(id);
        return article;
    }

    /*
     * 点击后增加浏览量
     * */
    @Override
    public void update(Integer id) {
        hostListMapper.updateViewCount(id);
    }

    /*
     * 点赞逻辑
     * */
    @Override
    public void like(Integer id) {

        // 获取用户详情
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        // 获取当前时间
        LocalDateTime date = LocalDateTime.now();
        // 把用户点赞的文章添加到用户点赞表中
        // 如果存在，则取消喜欢
        if (hostListMapper.selectLike(userId, id) > 0) {
            hostListMapper.removeLike(userId, id);
            hostListMapper.disLike(id);
            return;
        }
        hostListMapper.like(id);
        hostListMapper.addLike(userId, id, date);
    }

    @Override
    public List<Article> selectAll() {
        List<Article> list = hostListMapper.selectAll();
        return list;
    }

    @Override
    public List<Article> selectLike() {
        // 获取用户id
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List<Article> list = hostListMapper.selectLikeList(userId);
        return list;
    }
}
